package com.sylar.happygps.adapter;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.sylar.happygps.ContactEditActivity;
import com.sylar.happygps.R;

public class ContactAdapter extends BaseAdapter {
	private Activity context;
	public HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();
	private boolean hasDel = false;
	private boolean showItem2 = true;

	private int dataCount = 18;

	public ContactAdapter(Activity context) {
		this.context = context;

		for (int i = 0; i < dataCount; i++) {
			isSelected.put(i, false);
		}

	}

	public boolean getShowItem2() {
		return showItem2;
	}

	public void setShowItem2(boolean show) {
		this.showItem2 = show;
	}

	public boolean getDel() {
		return hasDel;
	}

	public void setDel(boolean del) {
		this.hasDel = del;
	}

	@Override
	public int getCount() {
		return dataCount;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		View favinavitem = context.getLayoutInflater().inflate(R.layout.contactitem, null);
		Button btn_del = (Button) favinavitem.findViewById(R.id.btn_del);
		ImageView btn_edit_contact = (ImageView) favinavitem.findViewById(R.id.btn_edit_contact);
		btn_edit_contact.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, ContactEditActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});

		if (hasDel)
			btn_del.setVisibility(View.VISIBLE);
		else
			btn_del.setVisibility(View.INVISIBLE);

		View contact_item_lable2 = favinavitem.findViewById(R.id.contact_item_lable2);

		if (showItem2) {
			contact_item_lable2.setVisibility(View.VISIBLE);
		} else {
			contact_item_lable2.setVisibility(View.GONE);
		}

		
		final CheckBox cb_contact_share_loc = (CheckBox) favinavitem.findViewById(R.id.cb_contact_share_loc);
		cb_contact_share_loc.setChecked(isSelected.get(arg0));
		cb_contact_share_loc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				isSelected.put(arg0, cb_contact_share_loc.isChecked());
			}
		});
		
		//假设第三条数据显示选择框，其它数据则不显示选择框
		if(arg0 == 2){
			cb_contact_share_loc.setVisibility(View.VISIBLE);
		}else{
			cb_contact_share_loc.setVisibility(View.GONE);
		}
		
		
		return favinavitem;
	}

}
